package com.example.friendcash.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.example.friendcash.R;
import com.example.friendcash.library.viewfinder.IViewFinder;
import com.example.friendcash.parameters.IntentConstants;

import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT;
import static com.example.friendcash.library.viewfinder.ViewFinders.getViewFinder;

public class NewEventActivity extends SherlockActivity {

    private EditText nameField;
    private EditText commentField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_event_activity);
        setTitle(R.string.new_event);
        final IViewFinder viewFinder = getViewFinder(this);
        nameField = viewFinder.findEditText(R.id.event_name);
        commentField = viewFinder.findEditText(R.id.event_comment);
    }

    @SuppressWarnings("UnusedDeclaration")
    public void createButtonHandler(MenuItem item) {
        if ( nameField.getText().length() <=0 ) {
            nameField.setError(getString(R.string.enter_name));
        } else {
            final Intent intent = new Intent();
            intent.putExtra(IntentConstants.NAME, nameField.getText().toString());
            intent.putExtra(IntentConstants.COMMENT, commentField.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.new_event_menu, menu);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setFocusToElement(nameField);
    }

    private void setFocusToElement(View view) {
        final Context applicationContext = getApplicationContext();
        final InputMethodManager inputManager = (InputMethodManager) applicationContext.getSystemService(INPUT_METHOD_SERVICE);
        inputManager.showSoftInput(view, SHOW_IMPLICIT);
    }

}
